package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import androidx.recyclerview.widget.RecyclerView;

import com.mingtu.guards.R;

import java.util.HashMap;
import java.util.List;


public class FilteAddressAdapter extends RecyclerView.Adapter<FilteAddressAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<String> mData;
    private HashMap<Integer, Boolean> hashMap = new HashMap<>(); //存放是否选中的状态，默认为false

    public FilteAddressAdapter(Context context, List<String> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filte, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.checkbox.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null && mData.size() > 0) {
            String text = mData.get(position);
            holder.checkbox.setText(text);

            holder.checkbox.setChecked(hashMap.get(position));
        }
        holder.checkbox.setTag(position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    /**
     * 更新数据
     *
     * @param position
     */
    public void upData(int position) {
        for (int i = 0; i < mData.size(); i++) {
            hashMap.put(i, false);
        }
        hashMap.put(position, true);
        notifyDataSetChanged();
    }

    public void upData(List<String> mData) {
        this.mData = mData;
        for (int i = 0; i < mData.size(); i++) {
            hashMap.put(i, false);
        }
        notifyDataSetChanged();
    }

    /**
     * 重置选中状态
     */
    public void resetData() {
        for (int i = 0; i < mData.size(); i++) {
            hashMap.put(i, false);
        }
        notifyDataSetChanged();
    }


    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        private final CheckBox checkbox;

        public ActivityHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);

        }
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
